import { reactive, inject } from "vue";
import { useProjectStore } from '../../../store/useProjectStore'

export const useProjectTeam = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;

    const teamData = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                projectName: ''
            }
        }
    );

    const projectStore = useProjectStore();

    //查询
    const getProjectTeams = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.GetProjectTeams,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { projectName: teamData.queryCondition.projectName },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                teamData.tableData = response.data.data;
                teamData.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //根据主记录ID获取团队资源详情
    const getTeamMembersByMId = async (mId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetTeamMembersByMId,
                {
                    mId: mId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                projectStore.teamModel = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    //根据项目ID获取团队资源详情
    const getTeamMembersByProjectId = async (projectId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.GetTeamMembersByProjectId,
                {
                    projectId: projectId
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                projectStore.teamModel = response.data.data;
            }
        } catch (error) {
            console.error(error);
        }
    }

    //制定团队资源
    const formulateProjectTeam = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.FormulateProjectTeam,
                projectStore.teamModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    //更新团队资源状态
    const updateTeamMainStatus = async (mainId, status) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.UpdateTeamMainStatus,
                {
                    mainId: mainId,
                    status: status
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }

    return {
        teamData,
        projectStore,
        getProjectTeams,
        getTeamMembersByMId,
        getTeamMembersByProjectId,
        formulateProjectTeam,
        updateTeamMainStatus
    };
};